package com.xinyiweidai.xinyiweidai.module.bind;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTextView;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.adapter.ContactsListAdapter;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.ContactsBean;
import com.xinyiweidai.xinyiweidai.bean.ContactsResultBean;
import com.xinyiweidai.xinyiweidai.event.BindingEvent;
import com.xinyiweidai.xinyiweidai.util.Api;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

/**
 * class:   ContactsListActivity
 * data：   2018/9/30  11:13
 * author:  LanX
 * Description:  通讯录列表
 *
 */
@SuppressWarnings("ALL")
public class ContactsListActivity extends BaseActivity {


    private List<ContactsBean> contactsList = new ArrayList<>();
    private RecyclerView recyclerView_contacts;
    private ContactsListAdapter adapter;
    private MyTextView tv_send;
    private int markContacts = 0;


    @Override
    protected void receiveData() {
        contactsList = (List<ContactsBean>) getIntent().getSerializableExtra("contacts_data");
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_contacts_list;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back, "联系人", 0, true);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        recyclerView_contacts = (RecyclerView) findViewById(R.id.recyclerView_contacts);
        recyclerView_contacts.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ContactsListAdapter(contactsList);
        recyclerView_contacts.setAdapter(adapter);
        tv_send = (MyTextView) findViewById(R.id.tv_send);
    }


    @Override
    protected void addClickEvent() {

        adapter.setOnItemCheckedListener((position, checked) -> {
            contactsList.get(position).setContact(checked ? "1" : "0");
            markContacts = markContacts + (checked ? 1 : -1);
        });

        tv_send.setOnClickListener(view -> {
            if (markContacts > 2) {
                MyToast.showError(ContactsListActivity.this, "只能选择两名联系人");
            } else if (markContacts < 2) {
                MyToast.showError(ContactsListActivity.this, "请选择两名联系人");
            } else {
                String json = new Gson().toJson(contactsList);
                sendContacts(json);
            }
        });
    }


    private void sendContacts(String json) {
        showLoading("正在认证");
        OkHttpUtils.post()
                .url(Api.contacts)
                .addParams("json", json)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        dismissLoading();
                        MyToast.showError(ContactsListActivity.this, "网络异常");
                    }

                    @Override
                    public void onResponse(String response) {
                        L.e(response);
                        dismissLoading();
                        parseData(response);
                    }
                });
    }


    private void parseData(String response) {
        ContactsResultBean bean = new Gson().fromJson(response, ContactsResultBean.class);
        if (bean.getCode() == 1) {
            MyToast.showSuccess(ContactsListActivity.this, "认证成功");
            EventBus.getDefault().post(new BindingEvent("status_3"));
            startNewActivity(OperatorActivity.class);
            finish();
        } else {
            MyToast.showError(ContactsListActivity.this, bean.getMessage());
        }
    }
}
